package scau.mis4.chanyulin.atm.view;

import javafx.scene.Cursor;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollBar;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import scau.mis4.chanyulin.atm.APP;
import scau.mis4.chanyulin.atm.base.common.CODE;
import scau.mis4.chanyulin.atm.base.common.PATH;
import scau.mis4.chanyulin.atm.base.utils.UiUtils;
import scau.mis4.chanyulin.atm.domain.Card;
import scau.mis4.chanyulin.atm.domain.Receipt;

import java.util.List;

/**
 * Created by chanyulin on 2018/4/27.
 * 用户物品栏
 */
public class CustomerBagView extends VBox {

    private HBox itemBox;
    private ScrollBar scrollBar;

    private int moneyLeft;
    private int cardLeft;
    private int receiptLeft;

    private Label totalInfo;

    // 图片资源
    private final ImageView MONEY_VIEW = new MoneyView();

    public CustomerBagView(int moneyLeft, List<Card> cardList) {
        itemBox = new HBox(10.0);
        totalInfo = new Label();

        this.moneyLeft = moneyLeft;
        this.cardLeft = cardList.size();
        this.receiptLeft = 0;
        updateTotalInfo();

        itemBox.getChildren().add(MONEY_VIEW);
        MONEY_VIEW.setCursor(Cursor.HAND);

        for (Card c : cardList) {
            itemBox.getChildren().addAll(new CardView(c));
        }

        // 添加滑动控制
        scrollBar = new ScrollBar();
        scrollBar.setVisibleAmount(25);
        scrollBar.valueProperty().addListener(e-> {
            double mov = getTotalItemNum() * 90 * 0.01 * scrollBar.getValue();
            itemBox.setTranslateX(-mov);
        });
        scrollBar.setOpacity(0.5);
        scrollBar.setCursor(Cursor.HAND);

        this.setSpacing(5);
        this.getChildren().addAll(itemBox, totalInfo, scrollBar);
    }

    /**
     * 更新物品统计信息
     */
    private void updateTotalInfo() {
        String info = "个人物品：现金" + moneyLeft + "元，银行卡" + cardLeft + "张，打印凭条" + receiptLeft + "张";
        totalInfo.setText(info);
    }

    /**
     * 添加金钱
     * @param money
     */
    public void addMoney(int money) {
        moneyLeft += money;
        updateTotalInfo();
    }

    /**
     * 添加银行卡
     * @param card
     */
    public void addCard(Card card) {
        cardLeft++;
        updateTotalInfo();
        itemBox.getChildren().add(new CardView(card));
    }

    /**
     * 添加凭条
     * @param receipt
     */
    public void addReceipt(Receipt receipt) {
        receiptLeft++;
        updateTotalInfo();
        itemBox.getChildren().add(new ReceiptView(receipt));
    }

    /**
     * 计算总物品数
     * @return
     */
    public int getTotalItemNum() {
        return cardLeft + receiptLeft + 1;
    }

    /**
     * 钞票图形
     */
    class MoneyView extends ImageView {
        private double startDragX;
        private double startDragY;
        private double anchorX;
        private double anchorY;
        private final Image RMB_IMG = UiUtils.getImage(PATH.RMB_HUNDRED_YUAN_IMG_PATH);

        public MoneyView() {
            this.setImage(RMB_IMG);
            this.setCursor(Cursor.HAND);
            this.setOnMousePressed(e -> {
                startDragX = this.getTranslateX();
                startDragY = this.getTranslateY();
                anchorX = e.getSceneX();
                anchorY = e.getSceneY();
            });
            this.setOnMouseDragged(e -> {
                double newTranslateX = startDragX + e.getSceneX() - anchorX;
                double newTranslateY = startDragY + e.getSceneY() - anchorY;
                this.setTranslateX(newTranslateX);
                this.setTranslateY(newTranslateY);
//                System.out.println(this.getLayoutX() + this.getTranslateX() + "， " + APP.cashierView.getLayoutX());
            });
            this.setOnMouseReleased(e -> {
//                 System.out.println(e.getSceneX() + "-" + APP.cashierView.getLayoutX());
//                 System.out.println(e.getSceneY() + "-" + APP.cashierView.getLayoutY());
                if (moneyLeft >= 100
                    && Math.abs(e.getSceneX() - APP.cashierView.getLayoutX() - 375) <= 15
                    && Math.abs(e.getSceneY() - APP.cashierView.getLayoutY() - 30) <= 15) {
                    moneyLeft -= 100;
                    updateTotalInfo();
                    APP.cashierView.putMoneyRequest(100);
                    this.setTranslateX(startDragX);
                    this.setTranslateY(startDragY);
                } else {
                    this.setTranslateX(startDragX);
                    this.setTranslateY(startDragY);
                }
            });
        }
    }

    /**
     * 银行卡图形
     */
    class CardView extends ImageView {
        private Card card;
        private double startDragX;
        private double startDragY;
        private double anchorX;
        private double anchorY;

        private Image IMG_AGRICULTURE_BANKCARD = UiUtils.getImage(PATH.BANKCARD_AGRICULTURE_IMG_PATH);
        private Image IMG_CHINA_BANKCARD = UiUtils.getImage(PATH.BANKCARD_CHINA_IMG_PATH);
        private Image IMG_COMMERCIAL_BANKCARD = UiUtils.getImage(PATH.BANKCARD_COMMERCIAL_IMG_PATH);
        private Image IMG_COMMUNICATION_BANKCARD = UiUtils.getImage(PATH.BANKCARD_COMMUNICATION_IMG_PATH);
        private Image IMG_CONSTRUCTION_BANKCARD = UiUtils.getImage(PATH.BANKCARD_CONSTRUCTION_IMG_PATH);

        public CardView(Card card) {
            this.card = card;
            // 根据不同的银行设置图像
            switch (card.getCompany()) {
                case 中国农业银行 : this.setImage(IMG_AGRICULTURE_BANKCARD); break;
                case 中国银行 : this.setImage(IMG_CHINA_BANKCARD); break;
                case 中国工商银行 : this.setImage(IMG_COMMERCIAL_BANKCARD); break;
                case 中国建设银行 : this.setImage(IMG_COMMUNICATION_BANKCARD); break;
                case 交通银行 : this.setImage(IMG_CONSTRUCTION_BANKCARD); break;
            }
            this.setCursor(Cursor.HAND);
            this.setOnMousePressed(e -> {
                if (2 == e.getClickCount()) {
                    new AlertStage("查看银行卡", "卡号：" + card.getCardId() + "\n银行：" + card.getCompany().toString()).show();
                    this.setTranslateX(startDragX);
                    this.setTranslateY(startDragY);
                } else {
                    startDragX = this.getTranslateX();
                    startDragY = this.getTranslateY();
                    anchorX = e.getSceneX();
                    anchorY = e.getSceneY();
                }
            });
            this.setOnMouseDragged(e -> {
                double newTranslateX = startDragX + e.getSceneX() - anchorX;
                double newTranslateY = startDragY + e.getSceneY() - anchorY;
                this.setTranslateX(newTranslateX);
                this.setTranslateY(newTranslateY);
//                 System.out.println(this.getLayoutX() + this.getTranslateX() + "， " + APP.cardReaderView.getLayoutX());
            });
            this.setOnMouseReleased(e -> {
//                 System.out.println(e.getSceneX() + "-" + APP.cardReaderView.getLayoutX());
//                 System.out.println(e.getSceneY() + "-" + APP.cardReaderView.getLayoutY());
                if (APP.cardReaderView.getImage() == APP.cardReaderView.CARD_EMPTY_IMG
                    && Math.abs(e.getSceneX() - APP.cardReaderView.getLayoutX() - 370) <= 15
                    && Math.abs(e.getSceneY() - APP.cardReaderView.getLayoutY() - 35) <= 15
                    && CODE.MSG_SUCCESS == insertCardRequest()) {

                    itemBox.getChildren().remove(this);
                    cardLeft--;
                    updateTotalInfo();

                    APP.loggerView.log("读卡器读卡成功，卡号：" + card.getCardId());
                    APP.cardReaderView.setImage(APP.cardReaderView.CARD_READING_IMG);
                    // 跳至密码输入页面
                    APP.monitorView.goToAuthorizePage();
                } else {
                    this.setTranslateX(startDragX);
                    this.setTranslateY(startDragY);
                }
            });
        }

        /**
         * 插卡请求
         * @return
         */
        public int insertCardRequest() {
            return APP.ATM.insertCardHandler(this.card);
        }

    }

    /**
     * 凭条图形
     */
    class ReceiptView extends ImageView {

        private double startDragX;
        private double startDragY;
        private double anchorX;
        private double anchorY;
        private Receipt receipt;

        public ReceiptView(Receipt receipt) {
            this.receipt = receipt;
            this.setImage(UiUtils.getImage(PATH.RECEIPT_IMG_PATH));
            this.setCursor(Cursor.HAND);
            // 显示凭条
            this.setOnMousePressed(e -> {
                if (2 == e.getClickCount()) {
                    new AlertStage("查看凭条", receipt.getInfomation()).show();
                    this.setTranslateX(startDragX);
                    this.setTranslateY(startDragY);
                } else {
                    startDragX = this.getTranslateX();
                    startDragY = this.getTranslateY();
                    anchorX = e.getSceneX();
                    anchorY = e.getSceneY();
                }
            });
            this.setOnMouseDragged(e -> {
                double newTranslateX = startDragX + e.getSceneX() - anchorX;
                double newTranslateY = startDragY + e.getSceneY() - anchorY;
                this.setTranslateX(newTranslateX);
                this.setTranslateY(newTranslateY);
            });
            this.setOnMouseReleased(e -> {
                this.setTranslateX(startDragX);
                this.setTranslateY(startDragY);
            });
        }
    }

}
